package com.facebook.friendlist;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.restfb.Connection;
import com.restfb.DefaultFacebookClient;
import com.restfb.FacebookClient;
import com.restfb.types.User;

@SuppressWarnings("serial")
public class MyFriendListServlet extends HttpServlet {
	private static final Logger log = Logger.getLogger(MyFriendListServlet.class.getName());
	private FacebookClient facebookClient;
	public void doPost(HttpServletRequest request, HttpServletResponse response) 
		throws ServletException, IOException {
		log.info("Attempt signed_request...");
		String signedRequest = (String) request.getParameter("signed_request");
		log.info("signedRequest :"+signedRequest);
		log.info("Create empty FacebookSignedRequest...");
		FacebookSignedRequest facebookSR = null;
		log.info("...success");
		try {
			log.info("Attempt getFacebookSignedRequest(signedReuest)...");
			facebookSR = FacebookSignedRequest.getFacebookSignedRequest(signedRequest);
			log.info("...success");
		} catch (Exception e) {
			log.warning("...failure. signed request getnt");
			e.printStackTrace();
		}
		log.info("Getting OauthToken...");
		String oauthToken = facebookSR.getOauth_token();
		PrintWriter writer = response.getWriter();
		
		if(oauthToken == null) {
			log.warning("...failure. token is null");
			response.setContentType("text/html");
			String authURL = "https://www.facebook.com/dialog/oauth?client_id="
								+ Constants.API_KEY + "&redirect_uri=https://apps.facebook.com/alexfriendlist/&scope=";
			log.info("Attempt go to facebook auth dialog and, then, back to our app...");
			writer.print("<script> top.location.href='"	+ authURL + "'</script>");
			writer.close();

		}
		else {
			log.info("...success");
			log.info("Create new facebook client...");
			facebookClient = new DefaultFacebookClient(oauthToken);
			log.info("...success");
			log.info("Attempt get friendlist by fetchConnection...");
			Connection<User> myFriends = facebookClient.fetchConnection("me/friends", User.class);
			log.info("...success");
			writer.print("<body bgcolor='yellow'>");
			writer.println("<b>If you see this text, then my simple facebook application works!</b>");
			writer.println("<b><h2><center><font color='red'>NU NI FIGA SEBE!!!</font></center></h2></b>");
			writer.println("<b><h3><center>Your friendlist:</center></h2></b>");
			writer.print("<center><table><tr><h3><th>Photo</th><th>Name</th><th>Identifier</th></h3></tr>");
			for (List<User> myFriendsList : myFriends) {
	
				for(User user: myFriendsList)
					writer.print("<tr><td><img src=\"https://graph.facebook.com/" + user.getId() + "/picture\"/></td><td>" + user.getName() +"</td><td>" + user.getId() + "</td></tr>");
	
			}
			writer.print("</table></center></body>");//writer.print("Error in Facebook classes restFB(((");
			writer.close();
			
		}

	}

}